package cc.comac.mom.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipRepairService;
import cc.comac.mom.bean.EquipRepair;
import cc.comac.mom.bean.EquipRepairBean;
import cc.comac.mom.bean.QualityAttachmentGroup;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.datahandlers.entity.EquipRepairDataHandler;
import cc.comac.mom.datahandlers.entity.QualityAttachmentGroupDataHandler;
import cc.comac.mom.entity.EquipRepairEntity;
import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.ExportExcel;
import cc.comac.mom.util.ResultBean;

@RestController
@Service
public class EquipRepairServiceImpl implements EquipRepairService {
	
	@Autowired
	private EquipRepairDataHandler equipRepairDataHandler;
	
	@Autowired
	private QualityAttachmentGroupDataHandler qualityAttachmentGroupDataHandler;

	@Resource
	private IdFactory idFactory;
	@Override
	public ResultBean selectByDevNumberAndFaultCause(@RequestBody EquipRepairBean obj) {
		
		ResultBean resultBean = new ResultBean(true);
		ResultDev resultDev = equipRepairDataHandler.selectByDevNumberAndFaultCause(obj);
		resultBean.setObject(resultDev);
		return resultBean;
	}
	@Override
	public ResultBean updateFaultCauseById(@RequestBody EquipRepair obj) {

		return equipRepairDataHandler.updateFaultCauseById(obj);
	}
	//提报
	@Override
	public ResultBean insetFaultInfo(@RequestBody EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		
		if (obj != null && obj.getInnerData() != null) {
			List<QualityAttachmentGroup> listAttachmentGroup = obj.getListAttachmentGroup();
			if (listAttachmentGroup != null && listAttachmentGroup.size() > 0) {
				long id = idFactory.generateId();
				obj.getInnerData().setAttachmentGroupId(id);
				for (QualityAttachmentGroup item : listAttachmentGroup) {
					item.getInnerData().setGroupId(id);
				}
				ResultBean resultBean2 = qualityAttachmentGroupDataHandler.insertBatch(listAttachmentGroup);
				if (!resultBean2.success) {
					throw new RuntimeException("保存附件组信息失败");
				}
			}
		}
		ResultBean resultBean2 = equipRepairDataHandler.insetFaultInfo(obj);
		if (!resultBean2.success) {
			throw new RuntimeException(resultBean2.getMessage());
		}

		return resultBean;
	}
	@Override
	public ResultBean getById(@RequestParam long id) {
		ResultBean resultBean = new ResultBean(true);
		EquipRepair byId = equipRepairDataHandler.getById(id);
		resultBean.setObject(byId);
		return resultBean;
	}
	@Override
	public ResultBean allot(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairDataHandler.allot(obj);
	}
	@Override
	public ResultBean acceptance(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairDataHandler.acceptance(obj);
	}
	@Override
	public ResultBean repairAnalyze(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairDataHandler.repairAnalyze(obj);
	}
	@Override
	public ResultBean repairBegin(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairDataHandler.repairBegin(obj);
	}
	@Override
	public ResultBean repairEnd(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairDataHandler.repairEnd(obj);
	}
	@Override
	public ResultBean dispatch(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairDataHandler.dispatch(obj);
	}
	@Override
	public ResultBean selectByCondition(@RequestBody EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev selectByCondition = equipRepairDataHandler.selectByCondition(obj);
		resultBean.setObject(selectByCondition);
		return resultBean;
	}
	@Override
	public String exportData(HttpServletRequest request, HttpServletResponse response, EquipRepair obj) {
		ResultDev selectByCondition = equipRepairDataHandler.selectByCondition(obj);
		@SuppressWarnings("unchecked")
		List<EquipRepair> list = (List<EquipRepair>) selectByCondition.getEntityList();
		String[] rowsName = new String[] { "工单编号", "使用部门", "设备编号", "设备名称", "所在位置", "具体位置","复发类型", "故障现象", "故障描述", "是否停机", "故障原因",
				"原因描述", "是否人为", "提报人", "产品质量", "是否委外", "故障发生时间", "预计完成时间", "维修开始", "故障结束", "维修时长", "维修组", "执行人","是否故障","执行状态",
				"验收人","验收时间","验收结果","是否返工","工单类型","维修影响设备精度","试运行情况","试件质量检验","首件质量检验","已自修内容","申请委外理由","申请委外内容","委外审核科室","审核技术员"};
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			Object[] objs = new Object[rowsName.length];//
			EquipRepairEntity entity = list.get(i).getInnerData();
			objs[0] = entity.getPlanNumber();
			objs[1] = entity.getUserDepartment();
			objs[2] = entity.getDevNumber();
			objs[3] = entity.getDevName();
			objs[4] = entity.getDevWorkShop();
			objs[5] = entity.getDevLocation();
			objs[6] = entity.getRecurrenceType();
			objs[7] = entity.getFaultDisplayDescribe();
			objs[8] = entity.getFaultDescribe();
			if(entity.getIsStop() == 0) {
				objs[9] = "否";
			}else {
				objs[9] = "是";
			}
			objs[10] = entity.getFaultCauseDescribe();
			objs[11] = entity.getCauseDescribe();
			if(entity.getIsArtificial() == 0) {
				objs[12] ="否";
			}else {
				objs[12] ="是";
			}
			objs[13] = entity.getReportUser();
			if(entity.getProductQuality() == 0) {
				objs[14] ="合格";
			}else {
				objs[14] ="不合格";
			}
			if(entity.getIsOutsource() == 0) {
				objs[15] = "不委外";
			}else {
				objs[15] = "委外";
			}
			
			objs[16] = entity.getFaultDate();
			objs[17] = entity.getExpectedCompletionDate();
			objs[18] = entity.getActualStartDate();

			objs[19] = entity.getActualEndDate();
			objs[20] = entity.getMaintenanceTime();
			objs[21] = entity.getRepairTeam();
			objs[22] = entity.getActualUser();

			objs[23] = entity.getIsFault();
			if(entity.getIsFault() == 0) {
				objs[23] ="否";
			}else {
				objs[23] ="是";
			}
			if(entity.getStatus() == 1) {
				objs[24] ="故障中";
			}else if(entity.getStatus() == 2) {
				objs[24] ="已派工";
			}else if(entity.getStatus() == 3) {
				objs[24] ="维修中";
			}else if(entity.getStatus() == 4) {
				objs[24] ="已维修";
			}else if(entity.getStatus() == 5) {
				objs[24] ="已验收";
			}else if(entity.getStatus() == 6) {
				objs[24] ="二次验收";
			}else {
				objs[24] ="";
			}
			
			objs[25] = entity.getAcceptanceUser();
			objs[26] = entity.getAcceptanceDate();
			if(entity.getAcceptanceResult() == 0) {
				objs[27] ="不合格";
			}else {
				objs[27] ="合格";
			}
			
			if(entity.getRemadeFlag() == 0) {
				objs[28] = "不返工";
			}else {
				objs[28] = "返工";
			}
			objs[29] = entity.getPlanType();
			
			if(entity.getRepairAffectPrecision()!= null && entity.getRepairAffectPrecision().equals("1")) {
				objs[30] = "是";
			}else if (entity.getRepairAffectPrecision()!= null && entity.getRepairAffectPrecision().equals("0")) {
				objs[30] = "否";
			}else {
				objs[30] = "";
			}
			if(entity.getTestResult()!= null && entity.getTestResult().equals("1")) {
				objs[31] = "正常";
			}else if (entity.getTestResult()!= null && entity.getTestResult().equals("0")) {
				objs[31] = "不正常";
			}else {
				objs[31] = "";
			}
			if(entity.getTestQualityCheck()!= null && entity.getTestQualityCheck().equals("1")) {
				objs[32] = "合格";
			}else if (entity.getTestQualityCheck()!= null && entity.getTestQualityCheck().equals("0")) {
				objs[32] = "不合格";
			}else {
				objs[32] = "";
			}
			if(entity.getInitialQualityCheck()!= null && entity.getInitialQualityCheck().equals("1")) {
				objs[33] = "合格";
			}else if (entity.getInitialQualityCheck()!= null && entity.getInitialQualityCheck().equals("0")) {
				objs[33] = "不合格";
			}else {
				objs[33] = "";
			}
			objs[34] = entity.getSelfTaughtContent();
			objs[35] = entity.getApplyingReason();
			objs[36] = entity.getApplyingContent();
			objs[37] = entity.getAuditDepartment();
			objs[38] = entity.getAuditUser();
			dataList.add(objs);
		}

		String fileName = "故障维修";
		// 执行导出
		String filePath = ExportExcel.exportExcel(request, response, fileName, rowsName, dataList,
				"yyyy-MM-dd HH:mm:ss");
		
		
		
		return filePath;

	}
	@Override
	public ResultBean outsourcingConfirm(@RequestBody EquipRepair obj) {
		ResultBean outsourcingConfirm = equipRepairDataHandler.outsourcingConfirm(obj);
		return outsourcingConfirm;
	}
	@Override
	public Map<String, String> printRepairInfoPdf(@RequestParam long repairId) throws Exception {
		Map<String, String> printRepairInfoPdf = equipRepairDataHandler.printRepairInfoPdf(repairId);
		return printRepairInfoPdf;
	}
	@Override
	public ResultBean queryExecuteCount(){
		ResultBean resultBean = new ResultBean(true);
		resultBean.setObject(equipRepairDataHandler.queryExecuteCount());
		return resultBean;
	}
}
